﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using DTcms.Common;
using System.Text;

namespace DTcms.Web.tools
{
    public partial class download1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        { 
            //取得处事类型
            string action = DTRequest.GetQueryString("action");
            int group_id = DTRequest.GetQueryInt("group_id");
            string keywords = DTRequest.GetQueryString("keywords");
            string referralcode = DTRequest.GetQueryString("referralcode");
            switch (action)
            {
                case "user_list": //用户导出excel 并下载
                    user_export(group_id, keywords, referralcode);
                    break;
                case "order_list"://用户升级订单导出excel 并下载
                    order_export(group_id, keywords);
                    break;
            }
                   
           
            
        }
        public void user_export(int _group_id,string _keywords,string _referralcode)
        {
            //string filename = "";
            BLL.users ubll = new BLL.users();
            StringBuilder strTemp = new StringBuilder();
            strTemp.Append("a.id>0 ");
            if (_group_id > 0)
            {
                strTemp.Append(" and group_id=" + _group_id);
            }
            _keywords = _keywords.Replace("'", "");
            if (!string.IsNullOrEmpty(_keywords))
            {
                strTemp.Append(" and (a.user_name like '%" + _keywords + "%' or a.mobile like '%" + _keywords + "%' or a.email like '%" + _keywords + "%' or a.nick_name like '%" + _keywords + "%' or a.referralcode like '%" + _keywords + "%')");
            }
            if (!string.IsNullOrEmpty(_referralcode))
            {
                strTemp.Append(" and ( a.referralcode like '%" + _referralcode + "%')");
            }
            DataSet ds = ubll.GetListForExport(5000, strTemp.ToString(), " a.id desc");
            //DataTable dt = ds.Tables[0];
            string dir = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);//取得当前桌面路径
            string exportfile = DateTime.Now.ToString("yyyyMMddHHmmss") + "所有会员";
            NPOIHelper.ExportByWeb(ds, "会员列表", dir + @"\" + exportfile + ".xls");
        }
        public void order_export(int _group_id,string _keywords) {
            //string filename = "";

            BLL.member_order mbll = new BLL.member_order();
            StringBuilder strTemp = new StringBuilder();
            strTemp.Append("a.id>0 ");
            if (_group_id > 0)
            {
                strTemp.Append(" and a.payment_status=" + _group_id);
            }
            _keywords = _keywords.Replace("'", "");
            if (!string.IsNullOrEmpty(_keywords))
            {
                strTemp.Append(" and (a.order_no like '%" + _keywords + "%' or a.parent_mobile like '%" + _keywords + "%')");
            }
            DataSet ds = mbll.GetListForExport(5000, strTemp.ToString(), " a.id desc");
            //DataTable dt = ds.Tables[0];
            string dir = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);//取得当前桌面路径
            string exportfile = DateTime.Now.ToString("yyyyMMddHHmmss") + "所有订单";
            NPOIHelper.ExportByWeb(ds, "会员升级订单", dir + @"\" + exportfile + ".xls");
        }
    }
}